<template>
  <div class='container'>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="文章标题:" prop="title" >
        <el-input v-model="ruleForm.title" placeholder="请输入文章标题"></el-input>
      </el-form-item>
      <el-form-item label="文章内容:" prop="articleBody">
        <quillEditor v-model="ruleForm.articleBody" :options="editorOption"/>
      </el-form-item>
      <el-form-item label="视频地址:">
        <el-input v-model="ruleForm.videoURL" placeholder="请输入视频地址"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="hClose">取消</el-button>
        <el-button type="primary" @click="onSubmit">确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, update } from '../../api/hmmm/articles'
export default {
  name: 'ArticleAdd',
  props: ['adata', 'isEdit'],
  components: { quillEditor },
  data () {
    const toolbar = [['bold', 'italic', 'underline', 'strike'], [{ list: 'ordered' }, { list: 'bullet' }], ['blockquote'], ['code-block', 'image', 'link']]
    return {
      editorOption: {
        placeholder: '',
        modules: {
          toolbar
        }
      },
      showDialog: false,
      ruleForm: {
        title: null,
        videoURL: null,
        articleBody: null
      },
      rules: {
        title: [{ required: true, message: '请输入文章标题', trigger: 'blur' }],
        articleBody: [{ required: true, message: '请输入文章内容', trigger: 'blur' }]
      }
    }
  },
  created () {
    if (this.isEdit === true) { this.ruleForm = JSON.parse(JSON.stringify(this.adata)) }
  },
  methods: {
    async doAdd () {
      const res = await add(this.ruleForm)
      console.log('doAdd', res)
      this.$message.success('添加成功')
      this.$emit('updata-add')
    },
    async doEdit () {
      const res = await update(this.ruleForm)
      this.$message.success('修改成功')
      console.log('doEdit', res)
      this.$emit('updata-add')
    },
    onSubmit () {
      console.log('onSubmit', this.adata)
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
        }
      })
    },
    hClose () {
      this.$emit('close')
    }
  }
}
</script>

<style scoped lang='scss'>
el-form-item {
  font-weight: 700;
}
::v-deep .ql-editor{
  min-height: 200px;
}

</style>
